#!/usr/bin/env bash

BIN_NAMES=(
    "openim-api"
    "openim-crontask"
    "openim-msggateway"
    "openim-msgtransfer"
    "openim-push"
    "openim-rpc-auth"
    "openim-rpc-conversation"
    "openim-rpc-friend"
    "openim-rpc-group"
    "openim-rpc-msg"
    "openim-rpc-third"
    "openim-rpc-user"
)

CHAT_NAMES=(
    "openim-admin-api"
    "openim-admin-rpc"
    "openim-bot-api"
    "openim-bot-rpc"
    "openim-chat-api"
    "openim-chat-rpc"
)

BIN_TARGET=""

# Function to help message
function cmd_help() {
    echo ".sh usage: $0 [options] [arg]"
    echo "options:"
    echo "  --pull            docker pull images"
    echo "  --pull-chat       docker pull chat images"
    echo "  --clear           docker rmi images"
    echo "  --clear-chat      docker rmi chat images"
    exit 1
}

# Function to check docker env
function check_openim_env() {
    if [ -f ".env" ]; then
       source .env
    else
       fatal ".env file not found"
    fi
    if [ -z "$IMAGE_REPOSITORY" ]; then
        fatal "IMAGE_REPOSITORY is not set"
        exit 1
    fi
    if [ -z "$IMAGE_VERSION" ]; then
        fatal "IMAGE_VERSION is not set"
        exit 1
    fi
    if [ -z "$CHAT_VERSION" ]; then
        fatal "CHAT_VERSION is not set"
        exit 1
    fi
}

# Function to push docker image
function pull_openim_image() {
  if [ -n "$BIN_TARGET" ]; then
    echo "Docker pull target: $BIN_TARGET"
    docker pull $IMAGE_REPOSITORY/$BIN_TARGET:$IMAGE_VERSION
  else
    for name in "${BIN_NAMES[@]}"; do
      echo "Docker pull item: $name"
      docker pull $IMAGE_REPOSITORY/${name}:$IMAGE_VERSION
    done
  fi
}

# Function to push docker image
function pull_chat_image() {
  if [ -n "$BIN_TARGET" ]; then
    echo "Docker pull target: $BIN_TARGET"
    docker pull $IMAGE_REPOSITORY/$BIN_TARGET:$CHAT_VERSION
  else
    for name in "${CHAT_NAMES[@]}"; do
      echo "Docker pull item: $name"
      docker pull $IMAGE_REPOSITORY/${name}:$CHAT_VERSION
    done
  fi
}

# Function to clear docker image
function clear_openim_image() {
  if [ -n "$BIN_TARGET" ]; then
    echo "Docker rmi target: $BIN_TARGET"
    docker rmi $IMAGE_REPOSITORY/$BIN_TARGET:$IMAGE_VERSION
  else
    for name in "${BIN_NAMES[@]}"; do
      echo "Docker rmi item: $name"
      docker rmi $IMAGE_REPOSITORY/${name}:$IMAGE_VERSION
    done
  fi
}

# Function to clear docker image
function clear_chat_image() {
  if [ -n "$BIN_TARGET" ]; then
    echo "Docker rmi target: $BIN_TARGET"
    docker rmi $IMAGE_REPOSITORY/$BIN_TARGET:$CHAT_VERSION
  else
    for name in "${CHAT_NAMES[@]}"; do
      echo "Docker rmi item: $name"
      docker rmi $IMAGE_REPOSITORY/${name}:$CHAT_VERSION
    done
  fi
}

# Entry function
function run() {
    if [ $# -eq 0 ]; then
        cmd_help
        exit 1
    fi

    if [ -n "$2" ]; then
      BIN_TARGET=$2
    fi

    check_openim_env

    while [ $# -gt 0 ]; do
        case $1 in
            -h|--help)
                cmd_help
                exit 1
                ;;
            --pull)
                pull_openim_image
                exit 1
                ;;
            --pull-chat)
                pull_chat_image
                exit 1
                ;;
            --clear)
                clear_openim_image
                exit 1
                ;;
            --clear-chat)
                clear_chat_image
                exit 1
                ;;
            *)
                echo "Unknown option: $1"
                cmd_help
                exit 1
                ;;
        esac
        shift
    done
}

# Run function
run "$@"
